
/**
 * CharacterCreationController class is the controller for the CharacterCreationWindow 
 * @author Team Love is like Sand
 * @version 1.0
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

/**
 * Controller for the character creation window
 * @author kushagramansingh
 * @version 1.0
 */
public class CharacterCreationController {
	
	/**
	 * The name of the character to be created 
	 */
	private String name;
	
	/**
	 * The difficulty of the game selected by the user
	 */
	private int difficulty;
	
	/**
	 * The total number of stat points 
	 */
	private int pointsRemaining = 16;
	
	/**
	 * The player object that will be created
	 */
	private Player player;
	
	/**
	 * dificultControl deals with the difficulty toggle buttons 
	 * @param difficulty that will be assigned
	 * @param other button to be disabled
	 * @param other2 button to be disabled
	 */
	public void difficultControl(int difficulty, JToggleButton other,
										JToggleButton other2){
		this.difficulty = difficulty;
		other.setSelected(false);
		other2.setSelected(false);
	}
	
	/**
	 * skillContol deals with dealing out different points to different skills while
	 * keeping it under the limit.
	 * @param val The value of points that will be left
	 * @param text The textfield that shows the points
	 * @param total The total points left
	 */
	public void skillControl(int val, JTextField text, JLabel total){
		if(pointsRemaining - val >= 0){
			String currValue = text.getText();
			Integer currVal = -1;
			try{
				currVal = Integer.parseInt(currValue);
			}
			catch(NumberFormatException e){
				e.getStackTrace();
			}
			if(currVal + val >= 0 && currVal + val <= 10){
				pointsRemaining -= val;
				currVal += val;
				text.setText(currVal.toString());
				total.setText("Points: "  + ((Integer) pointsRemaining).toString());
			}
		}
	}
	
	/**
	 * nextControl transitions from the Character Creation Window to the next screen
	 * @param frame The frame to be killed
	 * @param nameField Field that contains the player name
	 * @param textPilot Field that contains the piloting skill
	 * @param textEng Field that contains the engineering skill
	 * @param textFight Field that contains the fighting skill
	 * @param textTrade Field that contains the trading skill
	 */
	public void nextControl(JFrame frame, JTextField nameField, JTextField textPilot,
			JTextField textEng, JTextField textFight, JTextField textTrade){
		
		if(pointsRemaining == 0){
			JOptionPane.showMessageDialog(frame, "Welcome!You start with 10000 credits " +
															"and a Gnat.");
			int[] skills= {Integer.parseInt(textPilot.getText()),
					 Integer.parseInt(textEng.getText()),
					Integer.parseInt(textFight.getText()),
					 Integer.parseInt(textTrade.getText())};
			name = nameField.getText();
			player = new Player(skills, name, difficulty);
			frame.dispose();
		}
		else{
			 JOptionPane.showMessageDialog(frame, "Please assign all your points first!");
		}
	}
	
	/**
	 * getPlayer returns player
	 * @return player to be returned
	 */
	public Player getPlayer(){
		
		return player;
	}
	
}
